Destroying the Resources

Learn how to destroy all the resources that we created in Azure.

Destroying the resources#

We’re (almost) finished with the quick exploration of Terraform using AKS as an example. We saw how to add and change resources, and the only thing missing is to see how to destroy them.

If we want to delete some of the resources, all we’d have to do is remove their definitions and execute terraform apply. However, in some cases, we might want to destroy everything. There’s a command for that as well. However, in this specific case, we might not want to destroy everything.

We need to keep the storage where the Terraform state is stored. That will allow us to re-create the same cluster for the rest of the exercises. If we’re using AWS or GCP, we would simply execute terraform destroy because those two don’t allow us to destroy storage if there are files inside it. Or, to be more precise, that’s the default behavior, and we need to specify explicitly that we want to destroy storage even if it contains files by setting the argument force_destroy to true.

However, Azure does not have such a flag. So, if we execute terraform destroy, everything will be gone, including the storage with Terraform state. Since we want to keep that storage, we need to tell Terraform which targets to destroy, instead of wiping out everything.

In this case, we do want to destroy all the resources except the storage with Terraform state. We can do that through the --target argument. Fortunately for us, AKS is simple, and the whole cluster is defined as a single resource azurerm_kubernetes_cluster.

Command for destroying the resources

The output, limited to the relevant parts, is as follows.

Output of the command above

We can see that only the AKS cluster will be destroyed. Type “Yes” and press the “Enter” key. The output of the commands above is as follows.

Destroy complete! Resources: 1 destroyed.

The cluster and all the other resources we defined are now gone. The exception is the storage with the state that we left intact and will continue using in the exercises that follow.

Please note that we only removed the resources created through Terraform. Those that were created with az (e.g., resource group) are still there. Azure won’t charge us anything (or much) for them, so, unlike those we created with Terraform, there is no good reason to remove them. On the other hand, we might want to use the definitions from this chapter to create a cluster that will be used for the exercises in the others. Keeping those created with az will simplify the process. All we’ll have to do is execute terraform apply.

Try it yourself#

You can try all of the commands used in this lesson in the code playground below. Press the “Run” button and wait for a few seconds for it to connect.

For ease of use, all of the commands above are combined in main.sh.

Please provide values for the following:
AZURE_BUCKET_NAME
Not Specified...
/
main.sh
backend.tf
k8s-control-plane.tf
output.tf
provider.tf
storage.tf
variables.tf
Code playground

Reorganizing Definitions

Summary: AKS